//
//  DeleteTableViewController.m
//  11210-表格
//
//  Created by student on 16/3/4.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DeleteTableViewController.h"

@interface DeleteTableViewController ()
{
    BOOL bAdded;
}

@property (nonatomic,strong) NSMutableArray *list;
@end

@implementation DeleteTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.list = [@[@"张三",@"李四",@"王五"] mutableCopy];
    
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
    self.navigationItem.rightBarButtonItem = editBtn;
    
}

-(void)edit{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:self.tableView.editing?@"Done":@"Edit"];
    if (self.tableView.editing && bAdded == NO) {
        bAdded = YES;
        
        NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:self.list.count inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[tmpPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    if (self.tableView.editing == NO && bAdded == YES) {
        bAdded = NO;
        NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:self.list.count inSection:0];
        [self.tableView deleteRowsAtIndexPaths:@[tmpPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    if (bAdded) {
        return self.list.count + 1;
    }
    return self.list.count;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (indexPath.row == self.list.count) {
        cell.textLabel.text = @"添加新同学";
    }
    else{
    
        cell.textLabel.text = self.list[indexPath.row];
    }
   
    return cell;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.list.count) {
        return UITableViewCellEditingStyleInsert;
    }
    else{
        return UITableViewCellEditingStyleDelete;
    }
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //从数据中删除
        [self.list removeObjectAtIndex:indexPath.row];
        //表格视图中删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        // 1. 数据要添加
        [self.list addObject:[NSString stringWithFormat:@"新同学%i",self.list.count]];
        // 2. 新的一行
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
